/**
 * 业务字典项 列表页
 *
 * 作者 chenweihua
 * 日期 2020-05-22 15:28:32
 */
layui.config({
    base: ctx + 'febs/'
}).extend({
    commonJS:'lay/extends/common',
    febs: 'lay/modules/febs',
    validate: 'lay/modules/validate'
}).use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-dictItem'),
        $query = $view.find('#query'),
        $add = $view.find('#add'),
        $edit = $view.find('#edit'),
        $delete = $view.find('#delete'),
        $searchForm = $view.find('#dictItem-table-form'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'dictItemTable',
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'checkbox'},
                {field: 'itemCode', title: '项目编码', minWidth: 120},
                {field: 'itemName', title: '项目名称', minWidth: 120},
                {field: 'itemStatus', title: '状态', width: 100,align:'center',toolbar: '#status-bar' ,},
                {field: 'sortNum', title: '排序号', minWidth: 120, sort: true, edit: 'text'},
            ]],
            initSort: {
                field: 'sortNum' //排序字段，对应 dictItem 设定的各字段名
                , type: 'asc' //排序方式  asc: 升序、desc: 降序、null: 默认排序
            },
            autoSort: true
        });
        tableIns.reload({url: ctx + 'dictItem/list', where: getQueryParams(), page: {curr: 1}});
    }

    table.on('sort(dictItemTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    //监听单元格编辑
    table.on('edit(dictItemTable)', function (obj) {
        var value = obj.value //得到修改后的值
            , data = obj.data //得到所在行所有键值
            , field = obj.field; //得到字段
        //febs.alert.success('[ID: ' + data.itemId + '] ' + field + ' 字段更改为：' + value);
        var dictItem = {};
        dictItem.itemId = data.itemId;
        dictItem.sortNum = value;
        febs.post(ctx + 'dictItem/update', dictItem, function () {
            //layer.closeAll();
            //febs.alert.success('修改排序成功');
            $('#dictItem').find('#query').click();
        });

    });

    $query.on('click', function () {
        tableIns.reload({where: getQueryParams(), page: {curr: 1}});
    });

    $add.on('click', function () {
        febs.modal.open('新增', 'dictItem/add', {
            btn: ['提交', '取消'],
            area: $(window).width() <= 750 ? '90%' : '50%',
            yes: function (index, layero) {
                $('#dictItem-edit').find('#submit').trigger('click');
            },data: {
                dictCode: $searchForm.find('select[name="dictCode"]').val()
            },btn2: function () {
                layer.closeAll();
            },
        });
    });

    $edit.on('click', function () {
        var checkStatus = table.checkStatus('dictItemTable');
        if (checkStatus.data.length != 1) {
            febs.alert.warn('请选择一条需要修改的数据！');
        } else {
            var id = checkStatus.data[0].itemId;
            febs.modal.open('修改', 'dictItem/update/'+id, {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? '90%' : '50%',
                yes: function (index, layero) {
                    $('#dictItem-edit').find('#submit').trigger('click');
                },data: {
                    dictCode: $searchForm.find('select[name="dictCode"]').val()
                },btn2: function () {
                    layer.closeAll();
                }
            });
        }
    });

    $delete.on('click', function () {
        var checkStatus = table.checkStatus('dictItemTable');
        if (!checkStatus.data.length) {
            febs.alert.warn('没有选择要删除的数据！');
        } else {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                var ids = [];
                layui.each(checkStatus.data, function (key, item) {
                    ids.push(item.itemId);
                });
                deleteIds(ids.join(','));
            });
        }
    });

    function getQueryParams() {
        return {
            dictCode: $searchForm.find('select[name="dictCode"]').val(),
            itemName: $searchForm.find('input[name="itemName"]').val()?.trim(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'dictItem/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTabelHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });
});